package com.hyzx.qbasic.admin.domain.service.impl;

import com.github.pagehelper.PageInfo;
import com.hyzx.qbasic.admin.model.constant.CacheKeyConst;
import com.hyzx.qbasic.admin.model.po.RolePO;
import com.hyzx.qbasic.admin.model.vo.req.admin.AddRoleReq;
import com.hyzx.qbasic.admin.model.vo.req.admin.DeleteRoleReq;
import com.hyzx.qbasic.admin.model.vo.req.admin.EditRoleReq;
import com.hyzx.qbasic.admin.model.vo.req.admin.GetRoleListReq;
import com.hyzx.qbasic.admin.model.vo.res.BaseOperationRes;
import com.hyzx.qbasic.admin.common.util.OperationResUtils;
import com.hyzx.qbasic.admin.domain.repository.IRoleRepository;
import com.hyzx.qbasic.admin.domain.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Created by 黄江华 on 2017/10/17
 */

@Service
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS) //使用缓存需加此注解
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private IRoleRepository roleRepository;

    @Override
    public List<RolePO> getAllRoleList() {
        return roleRepository.getAllRoleList();
    }

    @Override
    public List<String> getAllRoleNameList() {
        List<RolePO> roleList = getAllRoleList();
        List<String> roleNameList = new ArrayList<>(roleList.size());

        if (!CollectionUtils.isEmpty(roleList))
            roleNameList = roleList.stream().map(role -> role.getRoleName())
                    .collect(Collectors.toList());

        return roleNameList;
    }

    @Override
    public Set<String> getUserRoleNameList(int userId) {
        return roleRepository.getUserRoleNameList(userId);
    }

    @Override
    public PageInfo<RolePO> getPagedRoleList(GetRoleListReq req) {
        PageInfo<RolePO> roleList = roleRepository.getPagedRoleList(req.getPageNum(),
                req.getPageSize(), req.getRoleName());

        return roleList;
    }

    @Override
    public BaseOperationRes addRole(AddRoleReq req) {
        //检查角色名称是否已经存在
        boolean isExisted = getAllRoleNameList().contains(req.getRoleName());
        if (isExisted)
            return OperationResUtils.fail("角色名已存在");

        RolePO po = new RolePO();
        po.setRoleName(req.getRoleName());
        po.setRoleDesc(req.getRoleDesc());
        po.setCreateTime(new Date());
        po.setUpdateTime(new Date());

        boolean success = roleRepository.addRole(po, req.getResourceList());
        if (!success)
            return OperationResUtils.fail("新增角色失败");

        return OperationResUtils.success();
    }

    @Override
    public List<Integer> getRoleResources(int roleId) {
        return roleRepository.getRoleResources(roleId);
    }

    @Override
    public RolePO getRoleInfo(int roleId) {
        return roleRepository.getRoleInfo(roleId);
    }

    @Override
    @CacheEvict(cacheNames = CacheKeyConst.PERMISSION_LIST_CACHE_NAME, allEntries = true)
    public BaseOperationRes editRole(EditRoleReq req) {
        //检查角色是否存在
        RolePO existedRole = roleRepository.getRoleInfo(req.getRoleId());
        if (existedRole == null)
            return OperationResUtils.fail("角色不存在");

        RolePO rolePO = new RolePO();
        rolePO.setRoleId(req.getRoleId());
        rolePO.setRoleName(req.getRoleName());
        rolePO.setRoleDesc(req.getRoleDesc());
        rolePO.setUpdateTime(new Date());

        boolean success = roleRepository.editRole(rolePO, req.getResourceList());
        if (!success)
            return OperationResUtils.fail("编辑角色信息失败");

        return OperationResUtils.success();
    }

    @Override
    @CacheEvict(cacheNames = CacheKeyConst.PERMISSION_LIST_CACHE_NAME, allEntries = true)
    public BaseOperationRes deleteRole(DeleteRoleReq req) {
        boolean success = roleRepository.deleteRole(req.getRoleIdList());
        if (!success)
            return OperationResUtils.fail("删除角色失败");

        return OperationResUtils.success();
    }
}